<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Logistics extends Controller
{
    public function expressfeealllist()
    {
        $this->db = new SysDb();
        $one = $this->db->table('goods_store')->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }
    public function expressfeelist()
    {
        $storeId  = trim(input('param.storeId'));
        $expressSheetId  = trim(input('param.expressSheetId'));
        $where=array(
            
            "store_id" => $storeId,
            "express_sheet_id" => $expressSheetId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tenantid = trim(input('param.tenantid'));
        $tol = (intval($page) - 1) * $limit;
        $admin = Db::table('sys_express_sheet')
            ->alias(' ses')
            ->join('sys_express_fee sef', 'ses.id = sef.express_sheet_id', "left")
            ->join('tenant_express_fee tef', 'tef.express_fee_id = sef.id', "left")
            ->where(['tenant_id' =>  $tenantid])
            ->where($where)
            ->limit($tol, $limit)
            // ->field(array(' tef.cost' =>'tcost','tef.profit'=>'tprofit','tef.id'=>"tid",'sef.max_weight','ses.express_status'))
            ->select();
        $admins = Db::table('sys_express_sheet')
            ->alias(' ses')
            ->join('sys_express_fee sef', 'ses.id = sef.express_sheet_id', "left")
            ->join('tenant_express_fee tef', 'tef.express_fee_id = sef.id', "left")
            ->where(['tenant_id' =>  $tenantid])
            ->where($where)
            // ->field(array(' tef.cost' =>'tcost','tef.profit'=>'tprofit','tef.id'=>"tid",'sef.max_weight','ses.express_status'))
            ->select();
        $len = count($admins);
        $data = array();
        array_push($data, $admin);
        array_push($data, $len);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function expresssheetalllist()
    {
        $id = trim(input('param.storeId'));
        $this->db = new SysDb();
        $one = $this->db->table('sys_express_sheet')->where(['store_id' =>  $id])->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }

    public function expressfeedel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $one = $this->db->table('sys_express_fee')->where(['id' =>  $id])->delete();
        if ($one) {
            exit(json_encode(['message' => 'succ', 'result' => "操作成功"]));
        }
    }

    public function expressfeeupdate()
    {
        $id = trim(input('param.id'));
        $tenantid = trim(input('param.tenantid'));
        $profit = trim(input('param.profit'));
        $this->db = new SysDb();
        $data = $this->db->table('tenant_express_fee')->where(['express_fee_id' =>  $id, 'tenant_id' => $tenantid])->item();

        $data['profit'] = $profit;
        $count = $this->db->table('tenant_express_fee')->where(['express_fee_id' =>  $id, 'tenant_id' => $tenantid])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
        }
    }


    public function expresssheetalllists()
    {

        $this->db = new SysDb();
        $one = $this->db->table('goods_store')->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }
    public function expresssheetlist()
    {
        $expressStatus  = trim(input('param.expressStatus'));
        $shipperName  = trim(input('param.shipperName'));
        $storeId  = trim(input('param.storeId'));
        $where=array(
            "express_status" => $expressStatus,
            "shipper_name" => $shipperName,
            "store_id" => $storeId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("sys_express_sheet")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('sys_express_sheet')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function expresssheetdel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('sys_express_sheet')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }





    public function storelist()
    {
        $name = trim(input('param.name'));
        $where=array(
            "name" => $name
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("goods_store")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('goods_store')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function storedel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('goods_store')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }




    public function expresssheetalllistes()
    {
        $this->db = new SysDb();
        $one = $this->db->table('sys_express_sheet')->lists();
        exit(json_encode(['message' => 'succ', 'result' => $one]));
    }


    public function viplevellist()
    {
        
        $status = trim(input('param.status'));
        $where=array(
            "status" => $status
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tenantid = trim(input('param.tenantid'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_vip_level")->order("create_time desc")->where($where)->where(['tenant_id' => $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_vip_level')->where($where)->where(['tenant_id' => $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function vipleveldel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('tenant_vip_level')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function viplevelupdateadd()
    {
        $id = input('param.from');
        $len = count($id);
        if ($len == 10) {
            $tenantid =  $id['tenant_id'];
            $name = $id['name'];
            $conditon_num = $id['condition_num'];
            $upgrade_amount = $id['upgrade_amount'];
            $rise = $id['rise'];
            $discount = $id['discount'];
            $is_default = $id['is_default'];
            $remark = $id['remark'];
            $status = $id['status'];
            $id = $id['id'];
            $this->db = new SysDb();
            $data = $this->db->table('tenant_vip_level')->where(['id' =>  $id])->item();
            //    $data['store_id']=$storeId;
            $data['remark'] = $remark;
            $data['status'] = $status;
            $data['tenant_id'] = $tenantid;
            $data['condition_num'] = $conditon_num;
            $data['upgrade_amount'] = $upgrade_amount;
            $data['rise'] = $rise;
            $data['discount'] = $discount;
            $data['name'] = $name;
            $data['is_default'] = $is_default;
            $count = $this->db->table('tenant_vip_level')->where(['id' =>  $id])->update($data);
            if ($count) {
                exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
            }
        } else {
            $tenantid =  $id['tenantid'];
            $name = $id['name'];
            $conditon_num = $id['condition_num'];
            $upgrade_amount = $id['upgrade_amount'];
            $rise = $id['rise'];
            $discount = $id['discount'];
            $is_default = 0;
            $remark = $id['remark'];
            $status = $id['status'];
            $this->db = new SysDb();

            //    $data['store_id']=$storeId;
            $data['remark'] = $remark;
            $data['status'] = $status;
            $data['tenant_id'] = $tenantid;
            $data['condition_num'] = $conditon_num;
            $data['upgrade_amount'] = $upgrade_amount;
            $data['rise'] = $rise;
            $data['discount'] = $discount;
            $data['name'] = $name;
            $data['is_default'] = $is_default;
            $count = $this->db->table('tenant_vip_level')->insert($data);
            if ($count) {
                exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
            }
        }
    }
}
